package com.flyme.module.file.provider.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.file.client.entity.SysCourseEnroll;
import com.flyme.module.file.provider.mapper.SysCourseEnrollMapper;
import com.flyme.module.file.provider.service.SysCourseEnrollService;
import org.springframework.stereotype.Service;

/**
 * @author: flyme
 * @date: 2018/3/7 15:27
 * @desc: 课程报名接口实现
 */
@Service
public class SysCourseEnrollServiceImpl extends BaseServiceImpl<SysCourseEnrollMapper, SysCourseEnroll> implements SysCourseEnrollService {

    /**
     * 查询用户是否申请/购买该课程
     */
    public Integer isApply(Long targetId, Long userId, Integer applyType){
        QueryWrapper queryWrapper = new QueryWrapper<SysCourseEnroll>();
        queryWrapper.eq("targetId", targetId);
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("applyType", applyType);
        return count(queryWrapper);
    }

    /**
     * 统计课程报名人数
     */
    public Integer applyNum(Long targetId,Integer applyType){
        QueryWrapper queryWrapper = new QueryWrapper<SysCourseEnroll>();
        queryWrapper.eq("targetId", targetId);
        queryWrapper.eq("applyType", applyType);
        return count(queryWrapper);
    }

    /**
     * 根据用户课程查询记录
     *
     */
    public SysCourseEnroll applyRecord(Long targetId, Long accountInfoId, Integer applyType){
        QueryWrapper queryWrapper = new QueryWrapper<SysCourseEnroll>();
        queryWrapper.eq("targetId", targetId);
        queryWrapper.eq("userId", accountInfoId);
        queryWrapper.eq("applyType", applyType);
        return getOne(queryWrapper, false);
    }

    /**
     * 查询客户所有已购买课程
     * @return
     */
    @Override
    public PqGrid findByUser(Long userId, PagerInfo pagerInfo){
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("u.avatar,u.userName,course.courseTitle,course.courseImage,course.recordNum,ce.targetId");
        cq.eq("applyType", 1);
        cq.eq("userId", userId);
        cq.createAlias("user", "user");
        cq.createAlias("course", "course");
        return pageList(cq);
    }
}
